<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
  <!-- Content Header (Page header) -->
  <section class="content-header">
    <h1>
      数据库上线
      <small>注意环境选择</small>
    </h1>
    <script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
    <ol class="breadcrumb">
      <li><a href="#"><i class="fa fa-dashboard"></i> Level</a></li>
      <li class="active">Here</li>
    </ol>
  </section>
  <!-- Main content -->
  <section class="content">

    <div class="row">
      <div class="col-xs-12">
            <form id="database_form" enctype="multipart/form-data" action="/sql_order/upload_file/<%= @db_name.dbname %>" method="post">
            <div class="box box-primary">
              <div class="box-header">
                <h3 class="box-title">数据库名：<%= @db_name.dbname %></h3>
              </div>
              <!-- /.box-header -->
              <div class="box-body">
                <div class="form-group<%= @title_error ? ' has-error' : '' %>">
                  <label>上线事由</label>
                  <input type="text" class="form-control" id="sql_title"  name="title"  placeholder="此处输入上线事由（至少四个字符,最长三十字符）" value="<%= params[:titles] %>" autocomplete="off">
                </div>
                <div class="form-group">
                  <!--<input type="file" name="filename" />-->
                  <input type="file" id="fileuploade" name="filename" style="width:300px;" onchange="selectFile(this)"/>
                </div>
                <!-- radio -->
                <div class="form-group">
                  <label>环境选择</label>
                  <% @db_envs.each do |env| %>
                      <div class="radio">
                        <label>
                          <input type="radio" name="database_id" value="<%= env.id %>"  checked>
                          <%= env.env %>
                        </label>
                      </div>
                  <%end%>

                </div>
              </div>
              <!-- /.box-body -->

              <div class="box-footer">
                <button type="button" class="btn btn-primary" id="btn_submit" onclick="clicked()">提交</button>
              </div>

            </div>
            </form>
            <!-- /.box -->
      </div>
      <!-- /.col -->
    </div>
    <!-- /.row -->
  </section>
  <!-- /.content -->
</div><!-- /.content-wrapper -->
</div>
<script type="text/javascript">
  $(function(){
    $('.ui-button-text').append('<span>select file</span>');
  });
  function selectFile(fnUpload){
      var filename = fnUpload.value;
      var mime = filename.toLowerCase().substr(filename.lastIndexOf("."));
      if(mime!=".sql")
      {
          alert("请选择sql格式的文件上传");
          fnUpload.outerHTML=fnUpload.outerHTML;
      }
  }
  var maxsize = 50*1024*1024;//50M
  var errMsg = "上传的附件文件不能超过50M！！！";
  var tipMsg = "您的浏览器暂不支持计算上传文件的大小，确保上传文件不要超过50M，建议使用IE、FireFox、Chrome浏览器。";
  var  browserCfg = {};
  var ua = window.navigator.userAgent;
  if (ua.indexOf("MSIE")>=1){
      browserCfg.ie = true;
  }else if(ua.indexOf("Firefox")>=1){
      browserCfg.firefox = true;
  }else if(ua.indexOf("Chrome")>=1){
      browserCfg.chrome = true;
  }
  function clicked(){
      console.log('click');
      if ($('.radio').find("input:checked").parent().text().trim() == '生产') {
        var re = window.confirm('该操作会提交到生产环境，确认继续吗？')
        if (!re) {
          return false;
          document.getElementById("btn_submit").innerHTML=false;
        }
      }
      var sql_title = $('#sql_title').val();
      var sql_title_length = sql_title.length;
      if(sql_title_length > 30){
        alert("上线事由长度不能超过30字符");
        return false;
      }else if(sql_title_length< 4){
        alert("上线事由长度不能少于4个字符");
        return false;
      }
      try{
          var obj_file = document.getElementById("fileuploade");
          if(obj_file.value==""){
              alert("请先选择上传文件");
              return;
          }
          var filesize = 0;
          if(browserCfg.firefox || browserCfg.chrome ){
              filesize = obj_file.files[0].size;
          }else if(browserCfg.ie){
              var obj_img = document.getElementById('tempimg');
              obj_img.dynsrc=obj_file.value;
              filesize = obj_img.fileSize;
          }else{
              alert(tipMsg);
              return;
          }
          if(filesize==-1){
              alert(tipMsg);
              return;
          }else if(filesize>maxsize){
              alert(errMsg);
              return;
          }
      }catch(e){
          alert(e);
      }
      $('#database_form').submit();
  };
</script>
